﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using Newtonsoft.Json;

namespace SQLLink.DomainClasses
{
    public class AddressComponent
    {
        public string long_name { get; set; }
        public string short_name { get; set; }
        public List<string> types { get; set; }
    }
    public class Northeast
    {
        public double lat { get; set; }
        public double lng { get; set; }
    }
    public class Southwest
    {
        public double lat { get; set; }
        public double lng { get; set; }
    }
    public class Bounds
    {
        public Northeast northeast { get; set; }
        public Southwest southwest { get; set; }
    }
    public class Location
    {
        public double lat { get; set; }
        public double lng { get; set; }
    }
    public class Viewport
    {
        public Northeast northeast { get; set; }
        public Southwest southwest { get; set; }
    }
    public class Geometry
    {
        public Bounds bounds { get; set; }
        public Location location { get; set; }
        public string location_type { get; set; }
        public Viewport viewport { get; set; }
    }
    public class Result
    {
        public List<AddressComponent> address_components { get; set; }
        public string formatted_address { get; set; }
        public Geometry geometry { get; set; }
        public List<string> types { get; set; }
    }
    public class RootObject
    {
        public List<Result> results { get; set; }
        public string status { get; set; }
    }

    public class GGM
    {
        public LatLon GetLatLon(string address)
        {
            var latlon = new LatLon();
            var url = "http://maps.googleapis.com/maps/api/geocode/json?address=" + address.Replace(" ", "%20") + "&sensor=false";
            WebResponse response = null;

            try
            {
                var request = (HttpWebRequest) WebRequest.Create(url);
                request.Method = "GET";
                response = request.GetResponse();
                var str = "";
                
                using (var stream = response.GetResponseStream())
                {
                    if (stream != null)
                        using (var streamReader = new StreamReader(stream))
                        {
                            str = streamReader.ReadToEnd();
                        }
                }

                var rootObject = JsonConvert.DeserializeObject<RootObject>(str);
                if (rootObject.status == "OK")
                {
                    var mtlResult = rootObject.results.FirstOrDefault(x => x.formatted_address.Contains("QC") 
                                                                   && x.types.Contains("intersection")) ??
                                    rootObject.results.FirstOrDefault(x => true);

                    if (mtlResult != null)
                    {
                        latlon.Latitude = mtlResult.geometry.location.lat;
                        latlon.Longitude = mtlResult.geometry.location.lng;
                    }
                }
            }
            catch
            {
                throw new Exception("JSON response failed.");
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }

            return latlon;
        }
    }
}